<template>
	<el-table :data="deviceData" style="width: 100%">
		<el-table-column prop="type" label="数据名称" width="150" />
		<el-table-column prop="current" label="当前值" width="115" />
		<el-table-column prop="normal" label="正常值" width="115" />
	</el-table>
</template>

<script >
export default {
	name:'ShowDeviceData',
	data(){
		return{
			LineID:'',
			LineName:'',
			deviceID:'',				//设备ID
			deviceName: '',				//设备名成
			deviceData:[],				//本设备所有传感器
			devicesData:[]				//本产线所有设别数据
		}
	},
	mounted(){
		console.log('ShowDeviceData_mounted')		//本组件已加载，打印路由传过来的设备名
		if(this.$route.query){
			this.deviceID = this.$route.query.deviceID								//存储设别ID
			this.deviceName = this.$route.query.deviceName							//存储设备名
		}
		else{
			this.deviceID = this.$store.state.currentPage.deviceID
			this.deviceName = this.$store.state.currentPage.deviceName
		}
		this.LineName = this.$store.state.currentPage.LineName
		this.LineID = this.$store.state.currentPage.LineID

		// console.log(this.$store.state.currentPage)

		var status = "";let data;
		if(this.$store.state.currentPage.devicesData.length !== 0){
				let devices = this.$store.state.currentPage.devicesData					//获取vuex中的当前产线里所有设备的数据
				this.devicesData = devices												//存储所有设备的数据
				// console.log('devices',devices)										//打印所有设别的数据

				//筛选所有设备的数据，筛选出本设别的数据
				data = devices.filter((item)=>{
					if(item.deviceID === this.deviceID){
						// console.log('item',item)
						// console.log('item.data',item.data)
						status = "found this device"
						return true									//true表明符合筛选条件保留本item，false抛弃
					}
				})
				console.log(status)
				if(status !== ""){
					// console.log('data',data[0].data)										//打印筛选出来的数据
					this.deviceData = data[0].data 											//存储筛选出来的数据中的传感器数据
					this.$store.state.currentPage.deviceData = data[0].data
				}
				else{
					//console.log('Not Found this device in that Line')
					this.$message({													//触发消息组件，提示用户找不到本设备
						showClose: true,
						message: 'Not Found this device in that Line',
						type: 'warning',
						delay:1000
					})
					window.setTimeout(() => {					 					//延迟，跳转到所有产线页面，让用户重新选择产线
						this.$router.push({
							name:'AllLine'
						})
						//console.log('end')
					}, 1000)
				}
		}
		else if(this.$store.state.currentPage.DeviceID !== ''){
			// console.log(this.$store.state.currentPage.DeviceID)
			// console.log(this.$store.state.productionLine)
			let Lines = this.$store.state.productionLine
			let Line = Lines.filter((line)=>{
				if(line.id === this.LineID){
					return true
				}
				else{
					return false
				}
			})
			// console.log('line:',Line)
			let devices = Line[0].devices
			// console.log('devices:',devices)

			//更新devices
			this.devicesData = devices
			this.$store.state.currentPage.devicesData = devices

			let device = Line[0].devices.filter((dev)=>{
				if(dev.deviceID === this.deviceID){
					return true
				}
				else{
					return false
				}
			})
			// console.log('device:',device)

			//更新device
			this.deviceData = device[0].data
			this.$store.state.currentPage.deviceData = device
		}
		else{
			//console.log('Not Found this device in that Line')
			this.$message({													//触发消息组件，提示用户找不到本设备
				showClose: true,
				message: 'Not Found this device in that Line',
				type: 'warning',
				delay:1000
			})
			window.setTimeout(() => {										//延迟，跳转到所有产线页面，让用户重新选择产线
				this.$router.push({
					name:'AllLine'
				})
				//console.log('end')
			}, 1000)
		}
	}
}
</script>
